#ifndef MANAGEDMEMORY_H_
#define MANAGEDMEMORY_H_

#include "../base/types.h"

template<class managedDataType>
class ManagedMemory
{
private:
	managedDataType* data;
	int* counter;

protected:
	managedDataType* getManagedMemoryData(){return data;}

	void init(managedDataType* data)
	{
		this->data = data;
		counter = new int;
		*counter = 1;
	}

	void unreference()
	{
		(*counter)--;
		if((*counter) == 0)
		{
			delete data;
			delete counter;
		}
	}

	void assign(const ManagedMemory<managedDataType>& data)
	{
		this->data = data.data;
		this->counter = data.counter;

		(*counter)++;
	}

public:
	virtual ~ManagedMemory()
		{unreference();}

};

#endif /* STRING_H_ */


